package com.cpecc.controller.wuzi;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cpecc.manager.system.UserManager;
import com.cpecc.manager.system.WuZiLBManager;
import com.cpecc.manager.wuzi.BaoFeiManager;
import com.cpecc.manager.wuzi.WeiXiuManager;
import com.cpecc.manager.wuzi.WuZiManager;
import com.cpecc.manager.wuzi.YiJiaoManager;
import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.WuZi;

@Controller
@RequestMapping(value = "/wuzi")
public class MyWuZiController {
	
	@Autowired
	private WuZiLBManager wuziLBManager;
	
	@Autowired
	private WuZiManager wuZiManager;
	
	@Autowired
	private UserManager userManager;
	
	@Autowired
	private WeiXiuManager weiXiuManager;
	
	@Autowired
	private YiJiaoManager yiJiaoManager;
	
	@Autowired
	private BaoFeiManager baoFeiManager;

	@RequestMapping(value = "/myList")
	public ModelAndView myList() {
		ModelAndView mav = new ModelAndView();
		mav.addObject("wuziLB1", wuziLBManager.getAllWuZiLB1());
		mav.setViewName("wuzi/myList");
		return mav;
	}
	
	//无用
	@RequestMapping(value = "/myAllList")
	public ModelAndView myAllList() {
		ModelAndView mav = new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("myWuZiList", wuZiManager.getMyWuZi(user));
		mav.setViewName("wuzi/myListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/myWuZiCX")
	public ModelAndView myWuZiCX(WuZi wuZi,String wuZiLBId,int wuZiZTId) {		
		ModelAndView mav = new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		List<WuZi> myWuZiList=wuZiManager.getMyListCX(wuZi, user,wuZiLBId,wuZiZTId);
		List<WuZi> result=new ArrayList<WuZi>();
		for(WuZi w:myWuZiList){
			if(weiXiuManager.hasWuZi(w.getId())){
				w.setPanDian(1);
			}else if(yiJiaoManager.hasWuZi(w.getId())){
				w.setPanDian(1);				
			}else if(baoFeiManager.hasWuZi(w.getId())){
				w.setPanDian(1);
			}else
				w.setPanDian(0);
			result.add(w);
		}
		mav.addObject("myWuZiList", result);
		mav.setViewName("wuzi/myListAjax");
		return mav;
	}
	
	
}
